package com.sylli.oeuf.server.db.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.CharacterPostDataTO;

public class MySQLCharacterPostData extends MySQLBasedProvider<CharacterPostDataTO> {

	public MySQLCharacterPostData() {
		interactTableName = "character_post_data";
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			CharacterPostDataTO obj) throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (char_id, current_pos, current_map, current_hp, current_mp, current_exp, turnleft, totalturn)"
					+ " VALUES (?,?,?,?,?,?,?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, obj.getCharId());
			ps.setInt(2, obj.getCurrentPos());
			ps.setInt(3, obj.getCurrentMap());
			ps.setInt(4, obj.getCurrentHp());
			ps.setInt(5, obj.getCurrentMp());
			ps.setInt(6, obj.getCurrentExp());
			ps.setInt(7, obj.getTurnLeft());
			ps.setLong(8, obj.getTotalTurn());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(
			CharacterPostDataTO obj) throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET current_pos=?, current_map=?, current_hp=?, current_mp=?, current_exp=?, turnleft=?, totalturn=? WHERE char_id = ?;");
			ps.setFloat(1, obj.getCurrentPos());
			ps.setInt(2, obj.getCurrentMap());
			ps.setInt(3, obj.getCurrentHp());
			ps.setInt(4, obj.getCurrentMp());
			ps.setInt(5, obj.getCurrentExp());
			ps.setInt(6, obj.getTurnLeft());
			ps.setLong(7, obj.getTotalTurn());
			ps.setInt(8, obj.getCharId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected CharacterPostDataTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			CharacterPostDataTO sto = new CharacterPostDataTO();
			rs.first();
			sto.setCharId(rs.getInt(1));
			sto.setCurrentPos(rs.getInt(2));
			sto.setCurrentMap(rs.getInt(3));
			sto.setCurrentHp(rs.getInt(4));
			sto.setCurrentMp(rs.getInt(5));
			sto.setCurrentExp(rs.getInt(6));
			sto.setTurnLeft(rs.getInt(7));
			sto.setTotalTurn(rs.getLong(8));
			return sto;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<CharacterPostDataTO> convertResultSetToObjects(
			ResultSet rs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<CharacterPostDataTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

}
